package com.zxl.yjb.activity.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.IncomeListBean;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;
import com.zxl.yjb.widget.ClearEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawalsAcitivity extends BaseActivity {

    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.edt_zfb_name)
    ClearEditText edtZfbName;
    @BindView(R.id.edt_account_number)
    ClearEditText edtAccountNumber;
    @BindView(R.id.edt_amount)
    ClearEditText edtAmount;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    private LoginSucessBean loginSucessBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawals);
        ButterKnife.bind(this);
        Object bean = SaveBeanUtil.getBean(this, Const.VIPINFO);
        if (bean != null) {
            loginSucessBean = (LoginSucessBean) bean;
        }
        initView();
        getIncomeList();
    }

    @Override
    protected void initView() {
        tvPageTitle.setText(R.string.withdrawals);
        if (loginSucessBean==null)
            return;
        tvBalance.setText("（可提现金额"+"*元）");
    }

    @OnClick({R.id.iv_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_submit:
                check();
                break;
        }
    }

    /**
     * 检查输入信息
     */
    private void check() {
        String money = edtAmount.getText().toString().trim();
        String alipayAccount = edtAccountNumber.getText().toString().trim();
        String zfbName = edtZfbName.getText().toString().trim();
        if (TextUtils.isEmpty(money)) {
            ToastUtils.show(getApplicationContext(),"金额不能为空");
            return;
        }
        if (TextUtils.isEmpty(alipayAccount)) {
            ToastUtils.show(getApplicationContext(),"支付宝姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(zfbName)) {
            ToastUtils.show(getApplicationContext(),"支付宝账户不能为空");
            return;
        }
        if (Double.valueOf(money) > 5000) {
            ToastUtils.show(getApplicationContext(),"金额过大");
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("money",money);
        map.put("alipay",alipayAccount);
        map.put("name",zfbName);
        withdrawals(map);
    }

    /**
     * 提现
     */
    private void withdrawals(Map<String, String> map) {
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object commenBeanList) {
                ToastUtils.show(WithdrawalsAcitivity.this,"提现成功");
                finish();
            }
        };
        dataManager.withdrawals(new ProgressObserver<Object>(listener,this),map);
    }


    private void getIncomeList()
    {
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<IncomeListBean> listener = new ObserverOnNextListener<IncomeListBean>() {
            @Override
            public void onNext(IncomeListBean incomeListBean) {
                tvBalance.setText("（可提现金额"+incomeListBean.getMay_tx()+"元）");
            }
        };
        dataManager.incomeList(new ProgressObserver<IncomeListBean>(listener,this),new HashMap<String, String>());
    }
}
